<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/23
 * Time: 15:35
 */

namespace Wechat\Controller;

/*前台用户控制器*/
class MineController extends CommonController
{
    /*前台用户主页*/
    public function index()
    {
        $data = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $this->assign('data',$data);
        $this->display();
    }
    /*成为会员*/
    public function vip()
    {
        $this->display();
    }
    /*绑定手机号*/
    public function phone()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布人手机号');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['phone'])){
                $this->error('手机号格式不正确');
            }
            $save = [
                'phone'=>$post['phone'],
            ];
            if(M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save($save)){
                $this->success('绑定成功',U('Webmine/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*申请月付*/
    public function yuefu()
    {
        $this->display();
    }
    /*添加收藏*/
    public function shoucang()
    {
        $id = i('get.id');
        $openid = $_SESSION['wechatopenid'];
        $data = [
            'openid'=>$openid,
            'time'=>time(),
            'tid'=>$id
        ];
        if($openid == NULL){
            $this->error('未知错误！请稍后重试');
        }else{
            if(M('shoucang')->add($data)){
                $this->success('收藏成功！');
            }else{
                $this->error('收藏失败');
            }
        }
    }
    /*个人删除*/
    public function del(){
        $id = i('get.id');
        if(M('zhuanjie')->where(['id'=>$id])->delete()){
            M('shoucang')->where(['tid'=>$id])->delete();
            $this->success('删除成功！');
        }else{
            $this->error('删除失败');
        }
    }
    /*个人置顶*/
    public function zhiding(){
        $id = i('get.id');
        if(M('zhuanjie')->where(['id'=>$id])->save(['sort'=>99999])){
            $this->success('置顶成功！');
        }else{
            $this->error('置顶失败');
        }
    }
    /*个人编辑*/
    public function edit()
    {

        $id = i('get.id');
        //大类
        $type = i('get.type');
        //小类
        $build = i('get.build');
        if(IS_POST){
            $post = i('post');

        }else{
            $this->assign(['type'=>$type,'build'=>$build]);
            $this->display();
        }
    }
    /*我的发布*/
    public function minefabu()
    {
        M('zhuanjie')->where(['time'=>time()])->save(['check'=>0]);
        /*出租*/
        $chuzu = M('zhuanjie')->where(['type'=>1,'openid'=>$_SESSION['wechatopenid']])->select();
        /*出售*/
        $chushou = M('zhuanjie')->where(['type'=>2,'openid'=>$_SESSION['wechatopenid']])->select();
        /*新房出售*/
        $xinfang = M('zhuanjie')->where(['type'=>3,'openid'=>$_SESSION['wechatopenid']])->select();
        $this->assign(['chuzu'=>$chuzu,'chushou'=>$chushou,'xinfang'=>$xinfang]);
        $this->display();
    }
    /*我的收藏*/
    public function mineshoucang()
    {
        $shoucang = M('shoucang')->where(['openid'=>$_SESSION['wechatopenid']])->order('id desc')->select();
        foreach ($shoucang as $k => $v){
            $shoucang[$k]['tid'] = M('zhuanjie')->where(['id'=>$v['tid']])->find();
        }
        /*
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        echo '<pre/>';
        var_dump($shoucang);
        die();
        */
        $this->assign('data',$shoucang);
        $this->display();
    }
    //分享过度
    public function guodu()
    {
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $user['tid'] = explode('|',$user['tid']);
        $friend = array_unique($user['tid']);

        foreach ($friend as $k => $v){
            $friend[$k] = M('wechatuser')->where(['openid'=>$v])->find();
            if(!$v){
                unset($friend[$k]);
            }
        }

        $this->assign(['v'=>$user,'f'=>$friend]);
        $this->display();
    }
    //返回数据
    public function fan()
    {
        $openid = i('post.openid');
        $xinfang = M('zhuanxinfang')->where(['openid'=>$openid])->order('id desc')->select();
        /*
        $xinfang = [
            'xinfang'=>$xinfang
        ];
        */
        echo json_encode($xinfang);
    }
    /*我要分享*/
    public function fenxiang()
    {
        $openie = i('get.openiv');
        $i = M('wechatuser')->where(['openid'=>$openie])->find();
        $iid = $i['id'];
        //永久二维码
        $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$iid.'}}}';
        $access_token = $this->access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;

        //获取到微信返回的信息
        $ti = $this->https($url,$qrcode);

        $ticketuc = urlencode($ti['ticket']);
            //拼接二维码
            $erwei = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $ticketuc;
            //获取用户的信息
            $user = M('wechatuser')->where(['openid' => $openie])->find();

            $url = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
            (new WechatController())->zidingfenxiang($url);
            $this->assign('openid', $user['openid']);
            $this->assign('v', $user);
            $this->assign('erwei', $erwei);
            $this->display();
    }
}